// pages/record/record.js
const db = wx.cloud.database({
    env: 'work1-0g1b3grp436e9236'
})
const dayjs = require('../../miniprogram_npm/miniprogram_npm/dayjs/index')
const util = require('../../utils/util')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isNull: false,
        jobList: [],
        loading: true,
        option1: [
            { text: '时间最晚', value: 0 },
            { text: '时间最早', value: 1 },
        ],
        value1: 0,
        curPage: 0,
        showDeleteAction: false,
        deleteActions: [
            { name: '删除', color: '#ee0a24' },
            { name: '取消'}
        ]
    },

    deleteCollection(e) {
        this.setData({showDeleteAction: true, deleteCollectionId: e.currentTarget.dataset.id})
    },

    onCloseDeleteActionSheet() {
        this.setData({showDeleteAction: false})
    },
    
    onSelectDeleteActionSheet({detail}) {
        const {name} = detail
        if (name === '删除') {
            this.setData({showDeleteAction: false})
            // 删除
            console.log(this.data.deleteCollectionId)
            db.collection('work_collect_job').doc(this.data.deleteCollectionId).update({
                data: {
                    status: 0
                }
            }).then(res=>{
                util.myToastSuccess('删除成功')
                this.setData({jobList: this.data.jobList.filter((item)=>item._id !== this.data.deleteCollectionId)})
                this.setData({deleteCollectionId: ''})
            })
        }
    },

    onSwitch1Change({detail}) {
        console.log(detail)
        this.setData({
            jobList: [],
            curPage: 0,
            loading: true
        })
        if (detail === 0) {
            this.getJobList()
        } else {
            this.getJobList('updateTime', 'asc')
        }
    },

    async getJobList(orderBy1='updateTime', order1='desc') {
        const userInfo = wx.getStorageSync('userInfo')
        let res = await db.collection('work_collect_job').skip(this.data.curPage * 10).limit(10).where({
            userId: userInfo.userId, status: 1
        }).orderBy(orderBy1, order1).get()
        const jobList = res.data
        // 最新
        for (let i = 0; i < jobList.length; i++) {
            const record = jobList[i]
            record.updateTime = dayjs(record.updateTime).format("YYYY-MM-DD HH:mm:ss")
        }
        this.setData({
            jobList: this.data.jobList.concat(jobList),
            loading: false
        })
        if (this.data.jobList.length === 0) {
            this.setData({isNull: true})
        }
    },

    toJobDetailPage(e) {
        console.log(e.currentTarget.dataset.id)
        wx.navigateTo({
            url: '/pages/job-detail/job-detail?id=' + e.currentTarget.dataset.id
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getJobList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
        wx.showNavigationBarLoading()
        this.setData({
            jobList: [],
            curPage: 0,
            loading: true
        })
        await this.getJobList()
        wx.hideNavigationBarLoading()
        // 数据成功后，停止下拉刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '加载中..',
        })
        this.setData({
            curPage: this.data.curPage + 1
        })
        this.getJobList().then(() => {
            // 数据成功后，停止下拉刷新
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})